package com.example.deviceinfo.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.deviceinfo.R
import com.example.deviceinfo.data.model.DeviceInfo
import com.example.deviceinfo.databinding.ItemDeviceBinding

class DeviceInfoAdapter :
    ListAdapter<DeviceInfo, DeviceInfoAdapter.DeviceViewHolder>(DeviceDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeviceViewHolder {
        val binding = ItemDeviceBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return DeviceViewHolder(binding)
    }

    override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) {
        val device = getItem(position)
        holder.bind(device) // 修复：调用bind方法
    }

    class DeviceViewHolder(private val binding: ItemDeviceBinding) :
        RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n", "ResourceAsColor")
        fun bind(device: DeviceInfo) {
            binding.apply {
                tvBrandModel.text = "${device.brand} ${device.model}"
                tvOs.text = "${device.os} ${device.osVersion}"
                tvCpuRam.text = "${device.cpu} | ${device.ram}"
                tvMac.text = "MAC: ${device.macAddress}"
                tvBattery.text = "电量: ${device.battery}%"

                // 设置系统图标
                val context = itemView.context
                ivSystemIcon.setImageResource(
                    when {
                        device.os.contains("Android", ignoreCase = true) -> R.drawable.ic_android
                        device.os.contains("iOS", ignoreCase = true) ||
                                device.os.contains("macOS", ignoreCase = true) ||
                                device.os.contains("watchOS", ignoreCase = true) -> R.drawable.ic_ios
                        device.os.contains("HarmonyOS", ignoreCase = true) -> R.drawable.ic_harmony
                        else -> R.drawable.ic_android
                    }
                )

                // 设置电量图标和颜色
                val batteryLevel = device.battery
                val batteryColor = when {
                    batteryLevel < 20 -> ContextCompat.getColor(context, R.color.red)
                    batteryLevel < 50 -> ContextCompat.getColor(context, R.color.orange)
                    else -> ContextCompat.getColor(context, R.color.green)
                }

                tvBattery.setTextColor(batteryColor)
                ivBatteryIcon.setColorFilter(batteryColor)
            }
        }
    }
}

class DeviceDiffCallback : DiffUtil.ItemCallback<DeviceInfo>() {
    override fun areItemsTheSame(oldItem: DeviceInfo, newItem: DeviceInfo): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: DeviceInfo, newItem: DeviceInfo): Boolean {
        return oldItem == newItem
    }
}